﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;

public class ColorHandler
{
    public static Color HSVtoColor(HSV hsv)
    {
        RGB rgb = HSVtoRGB(hsv);
        return Color.FromArgb(rgb.Red, rgb.Green, rgb.Blue);
    }

    public static Color HSVtoColor(int H, int S, int V)
    {
        return HSVtoColor(new HSV(H, S, V));
    }

    public static RGB HSVtoRGB(HSV HSV)
    {
        double num4 = 0.0;
        double num5 = 0.0;
        double num6 = 0.0;
        double num = ((((double) HSV.Hue) / 255.0) * 360.0) % 360.0;
        double num2 = ((double) HSV.Saturation) / 255.0;
        double num3 = ((double) HSV.value) / 255.0;
        if (num2 == 0.0)
        {
            num4 = num3;
            num5 = num3;
            num6 = num3;
        }
        else
        {
            double d = num / 60.0;
            int num11 = (int) Math.Floor(d);
            double num10 = d - num11;
            double num7 = num3 * (1.0 - num2);
            double num8 = num3 * (1.0 - (num2 * num10));
            double num9 = num3 * (1.0 - (num2 * (1.0 - num10)));
            switch (num11)
            {
                case 0:
                    num4 = num3;
                    num5 = num9;
                    num6 = num7;
                    goto Label_014D;

                case 1:
                    num4 = num8;
                    num5 = num3;
                    num6 = num7;
                    goto Label_014D;

                case 2:
                    num4 = num7;
                    num5 = num3;
                    num6 = num9;
                    goto Label_014D;

                case 3:
                    num4 = num7;
                    num5 = num8;
                    num6 = num3;
                    goto Label_014D;

                case 4:
                    num4 = num9;
                    num5 = num7;
                    num6 = num3;
                    goto Label_014D;

                case 5:
                    num4 = num3;
                    num5 = num7;
                    num6 = num8;
                    goto Label_014D;
            }
        }
    Label_014D:
        return new RGB((int) (num4 * 255.0), (int) (num5 * 255.0), (int) (num6 * 255.0));
    }

    public static RGB HSVtoRGB(int H, int S, int V)
    {
        return HSVtoRGB(new HSV(H, S, V));
    }

    public static HSV RGBtoHSV(RGB RGB)
    {
        double num7;
        double num8;
        double num4 = ((double) RGB.Red) / 255.0;
        double num5 = ((double) RGB.Green) / 255.0;
        double num6 = ((double) RGB.Blue) / 255.0;
        double num = Math.Min(Math.Min(num4, num5), num6);
        double num2 = Math.Max(Math.Max(num4, num5), num6);
        double num9 = num2;
        double num3 = num2 - num;
        if ((num2 == 0.0) || (num3 == 0.0))
        {
            num8 = 0.0;
            num7 = 0.0;
        }
        else
        {
            num8 = num3 / num2;
            if (num4 == num2)
            {
                num7 = (num5 - num6) / num3;
            }
            else if (num5 == num2)
            {
                num7 = 2.0 + ((num6 - num4) / num3);
            }
            else
            {
                num7 = 4.0 + ((num4 - num5) / num3);
            }
        }
        num7 *= 60.0;
        if (num7 < 0.0)
        {
            num7 += 360.0;
        }
        return new HSV((int) ((num7 / 360.0) * 255.0), (int) (num8 * 255.0), (int) (num9 * 255.0));
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct HSV
    {
        public int Hue;
        public int Saturation;
        public int value;
        public HSV(int H, int S, int V)
        {
            this.Hue = H;
            this.Saturation = S;
            this.value = V;
        }

        public override string ToString()
        {
            return string.Format("({0}, {1}, {2})", this.Hue, this.Saturation, this.value);
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RGB
    {
        public int Red;
        public int Green;
        public int Blue;
        public RGB(int R, int G, int B)
        {
            this.Red = R;
            this.Green = G;
            this.Blue = B;
        }

        public override string ToString()
        {
            return string.Format("({0}, {1}, {2})", this.Red, this.Green, this.Blue);
        }
    }
}

